﻿Imports System.Dynamic

Namespace Services

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Interface IDataProvider

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        ReadOnly Property Name As String

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="filter"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Function GetDataSources(ByVal filter As String) As IEnumerable(Of String)

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="sourcename"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Function GetDataMembers(ByVal sourcename As String) As IEnumerable(Of String)

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="member"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Function IsNumeric(ByVal member As String) As Boolean

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="member"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Function IsIndicator(ByVal member As String) As Boolean

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="source"></param>
        ''' <param name="members"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Function GetData(ByVal source As String, ByVal ParamArray members() As String) As IEnumerable

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="query"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Function GetData(ByVal query As Query) As IEnumerable

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Sub Refresh()

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Event Updated As EventHandler

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        ReadOnly Property SupportQuery As Boolean
    End Interface

    Friend Class TestProvider
        Implements IDataProvider


        Public Function GetData(ByVal query As Query) As System.Collections.IEnumerable Implements IDataProvider.GetData
            Throw New NotImplementedException()
        End Function

        Public Function GetData(ByVal source As String, ByVal ParamArray members() As String) As System.Collections.IEnumerable Implements IDataProvider.GetData

        End Function

        Public Function GetDataMembers(ByVal sourcename As String) As System.Collections.Generic.IEnumerable(Of String) Implements IDataProvider.GetDataMembers

        End Function

        Public Function GetDataSources(ByVal filter As String) As System.Collections.Generic.IEnumerable(Of String) Implements IDataProvider.GetDataSources

        End Function

        Public Function IsIndicator(ByVal member As String) As Boolean Implements IDataProvider.IsIndicator

        End Function

        Public Function IsNumeric(ByVal member As String) As Boolean Implements IDataProvider.IsNumeric

        End Function

        Public Sub Refresh() Implements IDataProvider.Refresh
            RaiseEvent Updated(Me, EventArgs.Empty)
        End Sub

        Public ReadOnly Property SupportQuery As Boolean Implements IDataProvider.SupportQuery
            Get
                Return False
            End Get
        End Property

        Public Event Updated(ByVal sender As Object, ByVal e As System.EventArgs) Implements IDataProvider.Updated

        Public ReadOnly Property Name As String Implements IDataProvider.Name
            Get

            End Get
        End Property
    End Class

    Public Class testdata

        Private Shared rnd As New Random(Now.Millisecond)

        Public Shared ReadOnly Property barcolumndata As IEnumerable
            Get
                Return _
            From i In Enumerable.Range(0, 4)
            Select c = "pos_" & i, y1 = 0.5 + 0.3 * rnd.NextDouble(), y2 = 0.5 + 0.4 * rnd.NextDouble()
            End Get
        End Property
    End Class

    Public Class ObjectProvider
        Implements IDataProvider

        Friend types As New List(Of Type)

        Sub New()

        End Sub

        Public Function GetData(ByVal query As Query) As System.Collections.IEnumerable Implements IDataProvider.GetData

        End Function
        Public Function GetData(ByVal source As String, ByVal ParamArray members() As String) As System.Collections.IEnumerable Implements IDataProvider.GetData


            Return testdata.barcolumndata
        End Function

        Public Function GetDataMembers(ByVal sourcename As String) As System.Collections.Generic.IEnumerable(Of String) Implements IDataProvider.GetDataMembers
            Return (From e In types Where e.Name = sourcename Take 1 Select e.GetProperties().Select(Function(p) p.Name)).FirstOrDefault()
        End Function
        Public Function GetDataSources(ByVal filter As String) As System.Collections.Generic.IEnumerable(Of String) Implements IDataProvider.GetDataSources
            Return From e In types Select e.Name
        End Function

        Public Function IsIndicator(ByVal member As String) As Boolean Implements IDataProvider.IsIndicator

        End Function

        Public Function IsNumeric(ByVal member As String) As Boolean Implements IDataProvider.IsNumeric

        End Function

        Public ReadOnly Property Name As String Implements IDataProvider.Name
            Get
                Return Me.GetType().Name
            End Get
        End Property

        Public Sub Refresh() Implements IDataProvider.Refresh

        End Sub

        Public ReadOnly Property SupportQuery As Boolean Implements IDataProvider.SupportQuery
            Get

            End Get
        End Property

        Public Event Updated(ByVal sender As Object, ByVal e As System.EventArgs) Implements IDataProvider.Updated
    End Class


    Public Class QueryableProvider(Of T)
        Implements IDataProvider

        Private queryable As IQueryable(Of T)

        Public Function GetData(ByVal query As Query) As System.Collections.IEnumerable Implements IDataProvider.GetData

        End Function

        Public Function GetData(ByVal source As String, ByVal ParamArray members() As String) As System.Collections.IEnumerable Implements IDataProvider.GetData

        End Function

        Public Function GetDataMembers(ByVal sourcename As String) As System.Collections.Generic.IEnumerable(Of String) Implements IDataProvider.GetDataMembers

        End Function

        Public Function GetDataSources(ByVal filter As String) As System.Collections.Generic.IEnumerable(Of String) Implements IDataProvider.GetDataSources

        End Function

        Public Function IsIndicator(ByVal member As String) As Boolean Implements IDataProvider.IsIndicator

        End Function

        Public Function IsNumeric(ByVal member As String) As Boolean Implements IDataProvider.IsNumeric

        End Function

        Public ReadOnly Property Name As String Implements IDataProvider.Name
            Get

            End Get
        End Property

        Public Sub Refresh() Implements IDataProvider.Refresh

        End Sub

        Public ReadOnly Property SupportQuery As Boolean Implements IDataProvider.SupportQuery
            Get

            End Get
        End Property

        Public Event Updated(ByVal sender As Object, ByVal e As System.EventArgs) Implements IDataProvider.Updated
    End Class


    Public Class AdoProvider
        Implements IDataProvider


        Private data As DataSet
        Private _p1 As String
        Private _data As DataSet

        Sub New(ByVal p1 As String, ByVal data As DataSet)
            ' TODO: Complete member initialization 
            _p1 = p1
            _data = data
        End Sub


        Public Function GetData(ByVal query As Query) As System.Collections.IEnumerable Implements IDataProvider.GetData

        End Function

        Public Function GetData(ByVal source As String, ByVal ParamArray members() As String) As System.Collections.IEnumerable Implements IDataProvider.GetData
            If members Is Nothing Then Return _data.Tables(source).DefaultView
            Return _data.Tables(source).DefaultView.ToTable(False, members).DefaultView
        End Function

        Public Function GetDataMembers(ByVal sourcename As String) As System.Collections.Generic.IEnumerable(Of String) Implements IDataProvider.GetDataMembers
            Return (From elt As DataColumn In _data.Tables(sourcename).Columns Select elt.ColumnName).ToArray()
        End Function


        Public Function GetDataSources(ByVal filter As String) As System.Collections.Generic.IEnumerable(Of String) Implements IDataProvider.GetDataSources
            Return (From elt As DataTable In _data.Tables Select elt.TableName).ToArray()
        End Function

        Public Function IsIndicator(ByVal member As String) As Boolean Implements IDataProvider.IsIndicator

        End Function

        Public Function IsNumeric(ByVal member As String) As Boolean Implements IDataProvider.IsNumeric

        End Function

        Public ReadOnly Property Name As String Implements IDataProvider.Name
            Get
                Return _p1
            End Get
        End Property

        Public Sub Refresh() Implements IDataProvider.Refresh

        End Sub

        Public ReadOnly Property SupportQuery As Boolean Implements IDataProvider.SupportQuery
            Get

            End Get
        End Property

        Public Event Updated(ByVal sender As Object, ByVal e As System.EventArgs) Implements IDataProvider.Updated
    End Class


    Public Class DynamicAdoProvider
        Implements IDataProvider


        Private _p1 As String

        Friend Shared Tables As New Dictionary(Of String, DataTable)

        Sub New(ByVal p1 As String)
            ' TODO: Complete member initialization 
            _p1 = p1
        End Sub


        Public Function GetData(ByVal query As Query) As System.Collections.IEnumerable Implements IDataProvider.GetData

        End Function

        Public Function GetData(ByVal source As String, ByVal ParamArray members() As String) As System.Collections.IEnumerable Implements IDataProvider.GetData
            If members Is Nothing Then Return Tables(source).DefaultView
            Return Tables(source).DefaultView.ToTable(False, members).DefaultView
        End Function

        Public Function GetDataMembers(ByVal sourcename As String) As System.Collections.Generic.IEnumerable(Of String) Implements IDataProvider.GetDataMembers
            Return (From elt As DataColumn In Tables(sourcename).Columns Select elt.ColumnName).ToArray()
        End Function


        Public Function GetDataSources(ByVal filter As String) As System.Collections.Generic.IEnumerable(Of String) Implements IDataProvider.GetDataSources
            Return (From elt As String In Tables.Keys Select elt).ToArray()
        End Function

        Public Function IsIndicator(ByVal member As String) As Boolean Implements IDataProvider.IsIndicator

        End Function

        Public Function IsNumeric(ByVal member As String) As Boolean Implements IDataProvider.IsNumeric

        End Function

        Public ReadOnly Property Name As String Implements IDataProvider.Name
            Get
                Return _p1
            End Get
        End Property

        Public Sub Refresh() Implements IDataProvider.Refresh

        End Sub

        Public ReadOnly Property SupportQuery As Boolean Implements IDataProvider.SupportQuery
            Get

            End Get
        End Property

        Public Event Updated(ByVal sender As Object, ByVal e As System.EventArgs) Implements IDataProvider.Updated
    End Class
End Namespace
